package torres.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.image.BufferedImage;
import java.util.ArrayList;


import torres.entity.Avatar;
import torres.entity.Entity;
import torres.map.World;

@SuppressWarnings("serial")
public class TwoDStatusViewPort extends AreaViewPort {
	
	private World world;
	private Avatar player;
	private int screenWidth;
	private int screenHeight;
	private int startXPixel;
	private int startYPixel;
	private int currentXPixel;
	private int currentYPixel;
	
	Font font;
	TextLayout textLayout;
	
	private ImageLoader loader;
	private BufferedImage heartImage;
	
	public TwoDStatusViewPort() {
		
		world = World.getInstance();
		player = world.getPlayer();
		
		loader = new ImageLoader("heart.png");
		heartImage = loader.getImage();
		
		this.startXPixel = 20;
		this.startYPixel = 35;
		this.currentXPixel = 0;
		this.currentYPixel = 0;
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.screenWidth = screenSize.width;
		this.screenHeight = screenSize.height;
		
		this.setPreferredSize(new Dimension(screenWidth, screenHeight));
		this.setIgnoreRepaint(true);
		
		setVisible(true);
		setFocusable(true);
	}

	public void paint(Graphics g) {
		player = World.getInstance().getPlayer();
		currentXPixel = startXPixel;
		currentYPixel = startYPixel;
		
		Graphics2D g2 = (Graphics2D)g;
		FontRenderContext frc = g2.getFontRenderContext();
		
		drawLivesLeft(g, frc);
		
		font = new Font("Helvetica",Font.BOLD, 16);
		textLayout = new TextLayout("Life", font, frc);
		
		g.setColor(Color.WHITE);
		g.fillRect(50, 50, 200, 20);
		g.setColor(Color.RED);
		g.fillRect(51, 51, (int) ((double) player.getCurrHealth() / player.getMaxHealth() * 200) - 2, 18);
		
		g2.setColor(Color.red);
		textLayout.draw(g2, 260, 60);
		
		g.setColor(Color.BLACK);
		g.fillRect(70, 70, 201, 21);
		g.setColor(Color.WHITE);
		g.fillRect(71, 71, 200, 20);
		g.setColor(Color.BLUE);
		g.fillRect(72, 72, (int) ((double) player.getCurrMana() / player.getMaxMana() * 200) - 2, 18);
		
		g2.setColor(Color.BLUE);
		textLayout = new TextLayout("Mana", font, frc);
		textLayout.draw(g2, 282, 80);
		
		g.setColor(Color.BLACK);
		g.fillRect(90, 90, 201, 21);
		g.setColor(Color.WHITE);
		g.fillRect(91, 91, 200, 20);
		g.setColor(Color.GREEN);
		int oldExperience = (int) Math.pow(player.getLevel()-1, 2.5);
		
		double pixWidth = 0;
		if(player.getExperience() > player.getExperienceReq()) {
			
			pixWidth = 200;
		}
		else {
			
			pixWidth = (((double) player.getExperience() - ((double) oldExperience)) / ((double) player.getExperienceReq() - (double) oldExperience) * 200);
		}
		
		g.fillRect(92, 92, (int) pixWidth - 2, 18);
		
		g2.setColor(Color.GREEN);
		textLayout = new TextLayout("Experience", font, frc);
		textLayout.draw(g2, 304, 100);
		
		g2.setColor(Color.GREEN);
		textLayout = new TextLayout("Gold: " + player.getGold(), font, frc);
		textLayout.draw(g2, (float) (screenWidth - textLayout.getBounds().getWidth() - 10), startYPixel);
		
		if( world.getCurrentMap().equals(world.getCombatArena())) {
			drawEnemyStats(g2);
		}
	}
	
	public void update(Graphics g) {
		paint(g);
	}
	
	public void drawEnemyStats(Graphics g) {
		Graphics2D g2 = (Graphics2D)g;
		ArrayList<Entity> enemyList = (ArrayList<Entity>)World.getInstance().getCombatArena().getEnemies();
		
		int dx,dy;		
		int i=1;
		int space=50;
		int distance;
		for(Entity enemy: enemyList) {
			dx = player.getLocation().getX() - enemy.getLocation().getX();
			dy = player.getLocation().getY() - enemy.getLocation().getY();
			distance = (int) Math.sqrt(dx*dx + dy*dy);
			
			g2.setFont(new Font("Helvetica", Font.BOLD, 18));
			g2.setColor(Color.green);
			
			g2.drawString(enemy.getDisplayName() + " " + i,
					screenWidth - screenWidth/8,
					i*(space));
			g2.drawLine(screenWidth-screenWidth/8 -2, i*space + 4,screenWidth-screenWidth/8 + 90, i*space + 4);
			
			g2.setFont(new Font("Helvetica", Font.BOLD, 12));
			g2.setColor(Color.magenta);
			
			if(player.getObservation() >= 1 && player.getObservation() <= 5) {
				g2.drawString("Health: " + 30/distance*enemy.getCurrHealth(),
						screenWidth - screenWidth/8 - 40,
						i*(space + 20));
			}
			else if(player.getObservation() >= 6 && player.getObservation() <= 10) {
				g2.drawString("Health: " + 50/distance*enemy.getCurrHealth(),
						screenWidth - screenWidth/8 -40,
						i*(space + 20));
				g2.drawString("Occupation: " + enemy.getOccupation(),
						screenWidth - screenWidth/8 -40,
						i*(space +40));
			}
			else if(player.getObservation() >= 11 && player.getObservation() <= 15) {
				g2.drawString("Health: " + enemy.getCurrHealth(),
						screenWidth - screenWidth/8 -40,
						i*(space + 20));
				g2.drawString("Occupation: " + enemy.getOccupation(),
						screenWidth - screenWidth/8 -40,
						i*(space + 40));
				g2.drawString("Skill: " + enemy.getSkillPoints(),
						screenWidth - screenWidth/8- 40,
						i*(space + 60));
			}
			else if(player.getObservation() >= 16) {
				g2.drawString("Health: " + enemy.getCurrHealth(),
						screenWidth - screenWidth/8-40,
						i*(space + 20));
				g2.drawString("Occupation: " + enemy.getOccupation(),
						screenWidth - screenWidth/8-40,
						i*(space + 40));
				g2.drawString("Skill: " + enemy.getSkillPoints(),
						screenWidth - screenWidth/8-40,
						i*(space + 60));
				g2.drawString("Element Resistance: " + enemy.getElementType(),
						screenWidth - screenWidth/8-40,
						i*(space + 80));
			}
			i++;
		}
	}
	
	public void drawLivesLeft(Graphics g, FontRenderContext frc) {
		player = World.getInstance().getPlayer();
		font = new Font("Helvetica",Font.BOLD, 24);
		textLayout = new TextLayout(player.getDisplayName() + " (" + player.getLevel() + ")" , font, frc);
		
		g.setColor(Color.YELLOW);
		textLayout.draw((Graphics2D) g, currentXPixel, currentYPixel);
		currentXPixel += textLayout.getBounds().getWidth() - startXPixel;
		
		g.drawImage(heartImage, currentXPixel, -20, null);
		currentXPixel += heartImage.getWidth() - 30;
		
		font = new Font("Helvetica",Font.BOLD, 24);
		textLayout = new TextLayout("x" + player.getLives(), font, frc);
		
		g.setColor(Color.RED);
		textLayout.draw((Graphics2D) g, currentXPixel, 40);
	}
}
